import { 
  ContainerConfig,
  ContainerEnv,
  DEFAULT_CONFIGS,
  SystemConfig
} from '../config/devcontainer';

export class ContainerGenerator {
  constructor(
    private env: ContainerEnv,
    private config: Partial<ContainerConfig> = DEFAULT_CONFIGS[env]
  ) {
    // 合并默认配置
    this.config = {
      ...DEFAULT_CONFIGS[env],
      ...config
    };
  }

  generateDockerfile(): string {
    const lines: string[] = [
      `FROM ${this.config.baseImage}`,
      '',
      '# 设置环境变量',
      `ENV TZ=${this.config.system?.timezone || 'UTC'} \\`,
      `    LANG=${this.config.system?.locale?.lang || 'en_US.UTF-8'} \\`,
      `    LC_ALL=${this.config.system?.locale?.lcAll || 'en_US.UTF-8'}`
    ];

    // 添加镜像源配置
    if (this.config.system?.mirrors) {
      lines.push(
        '',
        '# 配置镜像源',
        'ARG DEBIAN_FRONTEND=noninteractive',
        `ARG APT_MIRROR=${this.config.system.mirrors.apt}`,
        `ARG RUSTUP_MIRROR=${this.config.system.mirrors.rustup}`,
        `ARG CARGO_MIRROR=${this.config.system.mirrors.cargo}`
      );
    }

    // 安装系统工具
    if (this.config.toolchain?.basic || this.config.toolchain?.buildTools) {
      lines.push(
        '',
        '# 安装系统工具',
        'RUN set -eux; \\',
        '    apt-get update && \\',
        '    apt-get install -y --no-install-recommends \\'
      );

      const tools: string[] = [];
      
      if (this.config.toolchain?.basic) {
        tools.push('git', 'curl', 'ca-certificates', 'pkg-config');
      }
      
      if (this.config.toolchain?.buildTools) {
        tools.push('build-essential', 'cmake');
      }
      
      if (this.config.toolchain?.llvm) {
        tools.push('llvm', 'libclang-dev');
      }
      
      if (this.config.toolchain?.ssl) {
        tools.push('libssl-dev');
      }
      
      if (this.config.toolchain?.database) {
        tools.push(
          'libgflags-dev',
          'libsnappy-dev',
          'zlib1g-dev',
          'libbz2-dev',
          'libzstd-dev',
          'liblz4-dev'
        );
      }

      lines.push(
        ...tools.map(tool => `        ${tool} \\`),
        '    && apt-get clean \\',
        '    && rm -rf /var/lib/apt/lists/*',
        ''
      );
    }

    // Rust 配置
    if (this.env === 'dev' && this.config.rust) {
      lines.push(
        '# 配置 Rust 环境',
        'ENV PATH="/usr/local/cargo/bin:${PATH}" \\',
        '    CARGO_HOME=/usr/local/cargo \\',
        '    RUSTUP_HOME=/usr/local/rustup \\',
        '    CARGO_NET_GIT_FETCH_WITH_CLI=true',
        '',
        '# 安装 Rust 组件'
      );

      const components: string[] = [];
      if (this.config.rust.components?.rustAnalyzer) components.push('rust-analyzer');
      if (this.config.rust.components?.clippy) components.push('clippy');
      if (this.config.rust.components?.rustfmt) components.push('rustfmt');
      if (this.config.rust.components?.rustSrc) components.push('rust-src');

      if (components.length > 0) {
        lines.push(`RUN rustup component add ${components.join(' ')}`);
      }

      // Cargo 工具
      if (this.config.rust.tools?.cargoWatch) {
        lines.push('RUN cargo install cargo-watch');
      }
      if (this.config.rust.tools?.cargoEdit) {
        lines.push('RUN cargo install cargo-edit');
      }
    }

    // IDE 配置
    if (this.config.ide?.type !== 'none') {
      lines.push(
        '',
        '# IDE 配置',
        'RUN mkdir -p \\',
        '    /root/.vscode-server/extensions \\',
        '    /root/.vscode-server/data \\',
        '    /root/.cursor-server/extensions \\',
        '    /root/.cursor-server/data'
      );

      // SSH 配置
      if (this.config.ide?.ssh) {
        lines.push(
          '',
          '# SSH 配置',
          'RUN apt-get update && \\',
          '    apt-get install -y openssh-server && \\',
          '    mkdir -p /var/run/sshd && \\',
          '    echo "PermitRootLogin yes" >> /etc/ssh/sshd_config && \\',
          '    echo "PasswordAuthentication no" >> /etc/ssh/sshd_config && \\',
          '    echo "PubkeyAuthentication yes" >> /etc/ssh/sshd_config'
        );
      }
    }

    // 工作目录
    lines.push(
      '',
      '# 工作目录',
      'WORKDIR /app'
    );

    // 端口配置
    if (this.config.ports?.length) {
      lines.push(
        '',
        '# 端口配置',
        `EXPOSE ${this.config.ports.join(' ')}`
      );
    }

    return lines.join('\n');
  }
} 